package com.dalei.vdserver.webservice;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import com.dalei.vdserver.dao.DeviceInformationDao;
import com.dalei.vdserver.dao.DeviceStatusDao;
import com.dalei.vdserver.dao.SettingsDao;
import com.dalei.vdserver.dao.UserDao;
import com.dalei.vdserver.model.DeviceInformation;
import com.dalei.vdserver.model.DeviceStatus;
import com.dalei.vdserver.model.ErrorCode;
import com.dalei.vdserver.model.User;

public class SessionServiceImpl implements SessionService {

	private SettingsDao settingsDao;

	private UserDao userDao;

	private DeviceInformationDao deviceInformationDao;

	private DeviceStatusDao deviceStatusDao;

	public SettingsDao getSettingsDao() {
		return settingsDao;
	}

	public void setSettingsDao(SettingsDao settingsDao) {
		this.settingsDao = settingsDao;
	}
	
	public UserDao getUserDao() {
		return userDao;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	public DeviceInformationDao getDeviceInformationDao() {
		return deviceInformationDao;
	}

	public void setDeviceInformationDao(
			DeviceInformationDao deviceInformationDao) {
		this.deviceInformationDao = deviceInformationDao;
	}

	public DeviceStatusDao getDeviceStatusDao() {
		return deviceStatusDao;
	}

	public void setDeviceStatusDao(DeviceStatusDao deviceStatusDao) {
		this.deviceStatusDao = deviceStatusDao;
	}

	@Override
	public long getSessionTimeout() {
		// TODO Auto-generated method stub
		return settingsDao.getEnabledSettingsGroup().getSessionTimeout();
	}

	@Override
	public long requesSession(int deviceId, int userId) {
		// TODO Auto-generated method stub
		int i = 0;
		List<DeviceInformation> devices = deviceInformationDao.getDevices();
		for (i = 0; i < devices.size(); i++) {
			DeviceInformation deviceInformation = devices.get(i);
			if (deviceInformation.getId() == deviceId) {
				break;
			}
		}
		if (i == devices.size()) {
			return ErrorCode.Session.DEVICE_NOT_REGISTERED;
		}
		List<User> users = userDao.getUsers();
		for (i = 0; i < users.size(); i++) {
			User user = users.get(i);
			if (user.getId() == userId) {
				break;
			}
		}
		if (i == users.size()) {
			return ErrorCode.Session.USER_NOT_FOUND;
		}
		final long timeout = getSessionTimeout();
		DeviceStatus ds = deviceStatusDao.getStatusByDeviceId(deviceId);
		ds.setCurrentUserId(userId);
		Calendar calendar = new GregorianCalendar();
		Date expiredTime = new Date(calendar.getTimeInMillis() + timeout);
		ds.setExpiredTime(expiredTime);
		if (deviceStatusDao.updateStatus(ds) > 0) {
			return timeout;
		} else {
			return ErrorCode.INTERNAL_ERROR;
		}
	}

	@Override
	public boolean cancelSession(int deviceId) {
		// TODO Auto-generated method stub
		DeviceStatus ds = deviceStatusDao.getStatusByDeviceId(deviceId);
		ds.setExpiredTime(null);
		if (deviceStatusDao.updateStatus(ds) > 0) {
			return true;
		} else {
			return false;
		}
	}

}
